package com.sky.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * 柔性分布式事务的实现，mq相关配置
 */
@Configuration
public class RabbitMqBeanConfig {

    /**
     * 锁库存服务交换机
     **/
    @Bean
    public Exchange stockEventExchange() {
        return new TopicExchange("stock-event-exchange", true, false);
    }

    /**
     * 库存解锁队列
     **/
    @Bean
    public Queue stockReleaseQueue() {
        return new Queue("stock.release.queue", true, false, false);
    }

    /**
     * 延迟消息队列
     **/
    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 消息过期之后转发的交换机
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        // 消息过期之后转发的路由键
        arguments.put("x-dead-letter-routing-key", "stock.release");
        // 消息过期时间(2分)
        arguments.put("x-message-ttl", 120000);
        return new Queue("stock.delay.queue", true, false, false,arguments);
    }

    @Bean
    public Binding stockReleaseExchangeBinding() {
        return new Binding("stock.release.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.release.#", null);
    }

    @Bean
    public Binding stockCreateExchangeBinding() {
        return new Binding("stock.delay.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.locked", null);
    }
}
